#include "MCF52259_ADC_driver.h"
#include "var.h"

float pre;
float thi;

void ADC_init(void)
{
  
	MCF_GPIO_PANPAR=MCF_GPIO_PANPAR_AN0_AN0
					|MCF_GPIO_PANPAR_AN1_AN1
					|MCF_GPIO_PANPAR_AN2_AN2
					|MCF_GPIO_PANPAR_AN3_AN3
					|MCF_GPIO_PANPAR_AN4_AN4
					|MCF_GPIO_PANPAR_AN5_AN5
					|MCF_GPIO_PANPAR_AN6_AN6
					|MCF_GPIO_PANPAR_AN7_AN7;

	/* CTRL1: STOP0=1,START0=0,SYNC0=0,EOSIE0=0,ZCIE=0,LLMTIE=0,HLMTIE=0,CHNCFG=0,SMODE=2 */
  MCF_ADC_CTRL1 = 0x4002U;                            
  /* CTRL2: DIV=9 */
  MCF_ADC_CTRL2 = (unsigned short int)((MCF_ADC_CTRL2 & (unsigned short int)~0x16U) | 0x09U);
  /* CTRL2: STOP1=1,START1=0,SYNC1=0,EOSIE1=0,SIMULT=0,DIV=9 */
  MCF_ADC_CTRL2 = 0x4009U;                            
  /* ADSTAT: CIP0=0,CIP1=0,EOSI1=1,EOSI0=1,ZCI=0,LLMTI=0,HLMTI=0,RDY7=0,RDY6=0,RDY5=0,RDY4=0,RDY3=0,RDY2=0,RDY1=0,RDY0=0 */
  MCF_ADC_ADSTAT = 0x1800U;                            
  /* ADLSTAT: HLS7=1,HLS6=1,HLS5=1,HLS4=1,HLS3=1,HLS2=1,HLS1=1,HLS0=1,LLS7=1,LLS6=1,LLS5=1,LLS4=1,LLS3=1,LLS2=1,LLS1=1,LLS0=1 */
  MCF_ADC_ADLSTAT = 0xFFFFU;                            
  /* ADZCSTAT: ZCS7=1,ZCS6=1,ZCS5=1,ZCS4=1,ZCS3=1,ZCS2=1,ZCS1=1,ZCS0=1 */
  MCF_ADC_ADZCSTAT = 0xFFU;                            
  /* CTRL1: STOP0=0,START0=1 */
  MCF_ADC_CTRL1 = (unsigned short int)((MCF_ADC_CTRL1 & (unsigned short int)~0x4000U) | 0x2000U);
  /* CTRL2: STOP1=0,START1=1 */
  MCF_ADC_CTRL2 = (unsigned short int)((MCF_ADC_CTRL2 & (unsigned short int)~0x4000U) | 0x2000U);
  
  
  MCF_ADC_CTRL1|=MCF_ADC_CTRL1_EOSIE0;
  MCF_ADC_CTRL2|=MCF_ADC_CTRL2_EOSIE1;
  
//set interrupt for ADCA	
	MCF_INTC0_IMRH&=~MCF_INTC_IMRH_INT_MASK49;
	MCF_INTC0_IMRL&=~MCF_INTC_IMRL_MASKALL;
	MCF_INTC0_ICR49=MCF_INTC_ICR_IP(4)+MCF_INTC_ICR_IL(3);

//set interrupt for	ADCB
	MCF_INTC0_IMRH&=~MCF_INTC_IMRH_INT_MASK50;
	MCF_INTC0_IMRL&=~MCF_INTC_IMRL_MASKALL;
	MCF_INTC0_ICR50=MCF_INTC_ICR_IP(3)+MCF_INTC_ICR_IL(3);
	
}

void ADC_start(void)
{
	MCF_ADC_POWER&=~MCF_ADC_POWER_PD0;
	while(MCF_ADC_POWER&MCF_ADC_POWER_PSTS0) ;
	
	MCF_ADC_CTRL1&=~MCF_ADC_CTRL1_STOP0;
	MCF_ADC_CTRL1|=MCF_ADC_CTRL1_START0;
	
	MCF_ADC_POWER&=~MCF_ADC_POWER_PD1;
	while(MCF_ADC_POWER&MCF_ADC_POWER_PSTS1) ;
	
	MCF_ADC_CTRL2&=~MCF_ADC_CTRL2_STOP1;
	MCF_ADC_CTRL2|=MCF_ADC_CTRL2_START1;
}

void ADC_stop(void)
{
	MCF_ADC_CTRL1|=MCF_ADC_CTRL1_STOP0;
}

//source:49 ADCA conversion complete
__declspec(interrupt) void ADC0(void) 
{
	static unsigned int AD5;
	CT_INT_ADC0++;
	if(CT_INT_ADC0%225000==0)
		change(LED,LED1);
	DC3=(DC3*1+(0x8000-MCF_ADC_ADRSLT6))/2;
    DC2=(DC2*1+(0x8000-MCF_ADC_ADRSLT5))/2;
    DC1=(DC1*1+(0x8000-MCF_ADC_ADRSLT4))/2;
    pre=thi;
    thi=DC1-DC3;
    
//    DC_Control_D=(DC_Control_D*59+thi-pre)/60;
    
    DC_gyro=(DC_gyro*0+(MCF_ADC_ADRSLT0)*0);
    gyro=gyro*0.9+MCF_ADC_ADRSLT1;
    
    //------------------------------------------------
    //------------------------------------------------
    //------------------------------------------------
    //Temp is AN2
    //Battary is AN3
    //------------------------------------------------
    //------------------------------------------------
    
	MCF_ADC_ADSTAT|=MCF_ADC_ADSTAT_EOSI0;
}

//source:50 ADCB conversion complete
__declspec(interrupt)void ADC1(void)
{
	CT_INT_ADC1++;
	change(LED,LED2);
	/************************************
	 * 
	 *  	This interrupt should not be
	 *	used in usual.
	 */
	MCF_ADC_ADSTAT|=MCF_ADC_ADSTAT_EOSI1;	
}

//source:51 ADC  interrupt of ZCI LLMIT HLMIT
__declspec(interrupt)void ADCRANGE(void)
{
	CT_INT_ADCRANGE++;
	if(MCF_ADC_ADZCSTAT&MCF_ADC_ADZCSTAT_ZCS0)
	{
		MCF_ADC_ADZCSTAT|=MCF_ADC_ADZCSTAT_ZCS0;
	}
	if(MCF_ADC_ADLSTAT&MCF_ADC_ADLSTAT_LLS0)
	{
		MCF_ADC_ADLSTAT|=MCF_ADC_ADLSTAT_LLS0;
	}
	if(MCF_ADC_ADLSTAT&MCF_ADC_ADLSTAT_HLS0)
	{
		MCF_ADC_ADLSTAT|=MCF_ADC_ADLSTAT_HLS0;
	}
	/************************************
	 * 
	 *  	This interrupt should not be
	 *	used in usual.
	 */
}